import Vue from "vue";
import Router from "vue-router";

Vue.use(Router);

/* Layout */
import Layout from "@/layout";

/* 静态路由 */
export const constantRoutes = [
  {
    path: "/login",
    name: "login",
    component: () => import("@/views/login/index"),
    hidden: true,
  },
  {
    path: "/editPwd",
    name: "editPwd",
    component: Layout,
    hidden: true,
  },
  {
    path: "/recharge",
    name: "recharge",
    component: () => import("@/views/recharge/index"),
    hidden: true,
  },
  {
    path: "/rechargeCode",
    name: "rechargeCode",
    component: () => import("@/views/recharge/code"),
    hidden: true,
  },
  {
    path: "/rechargeSuccess",
    name: "rechargeSuccess",
    component: () => import("@/views/recharge/success"),
    hidden: true,
  },
  {
    path: "/apply",
    name: "apply",
    component: () => import("@/views/apply/index"),
    hidden: true,
  },
  {
    path: "/trans",
    component: () => import("@/views/trans"),
    hidden: true,
  },
  {
    path: "/401",
    component: () => import("@/views/error-page/401"),
    hidden: true,
  },
  {
    path: "/404",
    component: () => import("@/views/error-page/404"),
    hidden: true,
  },
  {
    path: "/",
    component: Layout,
    redirect: "/trans",
    hidden: true,
  },
];

/* 动态路由 */
export const asyncRoutes = [
  {
    path: "/service",
    name: "service",
    component: Layout,
    redirect: "/service/contract",
    meta: { title: "增值服务", icon: "service" },
    children: [
      {
        path: "message",
        name: "serviceMessage",
        component: () => import("@/views/service/message"),
        meta: { title: "短信提醒", icon: "", keepAlive: true },
      },
      {
        path: "messageRecord",
        name: "serviceMessageRecord",
        hidden: true,
        component: () => import("@/views/service/messageRecord"),
        meta: {
          title: "短信购买记录",
          icon: "",
          activeMenu: "/service/message",
          parentTitle: "短信提醒",
          parentRouteName: "serviceMessage",
        },
      },
      {
        path: "notarization",
        name: "serviceNotarization",
        component: () => import("@/views/service/notarization"),
        meta: { title: "协议公证", icon: "", keepAlive: true },
      },
      {
        path: "notarizationRecord",
        name: "serviceNotarizationRecord",
        hidden: true,
        component: () => import("@/views/service/notarizationRecord"),
        meta: {
          title: "协议公证记录",
          icon: "",
          activeMenu: "/service/notarization",
          parentTitle: "协议公证",
          parentRouteName: "serviceNotarization",
        },
      },
      {
        path: "mobile",
        name: "serviceMobile",
        component: () => import("@/views/service/mobile"),
        meta: { title: "手机号验证", icon: "", keepAlive: true },
      },
      {
        path: "mobileRecord",
        name: "serviceMobileRecord",
        hidden: true,
        component: () => import("@/views/service/mobileRecord"),
        meta: {
          title: "手机号验证记录",
          icon: "",
          activeMenu: "/service/mobile",
          parentTitle: "手机号验证",
          parentRouteName: "serviceMobile",
        },
      },
      {
        path: "radar",
        name: "serviceRadar",
        component: () => import("@/views/service/radar"),
        meta: { title: "雷达风控", icon: "", keepAlive: true },
      },
      {
        path: "radarRecord",
        name: "serviceRadarRecord",
        hidden: true,
        component: () => import("@/views/service/radarRecord"),
        meta: {
          title: "雷达风控记录",
          icon: "",
          activeMenu: "/service/radar",
          parentTitle: "雷达风控",
          parentRouteName: "serviceRadar",
        },
      },
      {
        path: "education",
        name: "serviceEducate",
        component: () => import("@/views/service/education"),
        meta: { title: "学历风控", icon: "", keepAlive: true },
      },
      {
        path: "educationRecord",
        name: "serviceEducationRecord",
        hidden: true,
        component: () => import("@/views/service/educationRecord"),
        meta: {
          title: "学历风控记录",
          icon: "",
          activeMenu: "/service/education",
          parentTitle: "学历风控",
          parentRouteName: "serviceEducate",
        },
      },
      {
        path: "ab",
        name: "serviceAb",
        component: () => import("@/views/service/ab"),
        meta: { title: "还款风控", icon: "", keepAlive: true },
      },
      {
        path: "abRecord",
        name: "serviceAbList",
        hidden: true,
        component: () => import("@/views/service/abRecord"),
        meta: {
          title: "还款风控记录",
          icon: "",
          activeMenu: "/service/ab",
          parentTitle: "还款风控",
          parentRouteName: "serviceAb",
        },
      },
      {
        path: "risk",
        name: "serviceRisk",
        component: () => import("@/views/service/risk"),
        meta: { title: "百融风控", icon: "" },
      },
      {
        path: "riskRecord",
        name: "serviceRiskRecord",
        hidden: true,
        component: () => import("@/views/service/riskRecord"),
        meta: {
          title: "风控记录",
          icon: "",
          activeMenu: "/service/risk",
          parentTitle: "百融风控",
          parentRouteName: "serviceRisk",
        },
      },
      {
        path: "booleaRisk",
        name: "serviceBooleaRisk",
        component: () => import("@/views/service/booleaRisk"),
        meta: { title: "布尔数据", icon: "" },
      },
      {
        path: "creditRisk",
        name: "serviceCreditRisk",
        component: () => import("@/views/service/creditRisk"),
        meta: { title: "征信数据", icon: "" },
      },
      {
        path: "insurance",
        name: "leaseInsurance",
        component: () => import("@/views/service/insurance"),
        meta: { title: "订单保险信息", icon: "" },
      },
      {
        path: "creditRiskRecord",
        name: "serviceCreditRiskRecord",
        hidden: true,
        component: () => import("@/views/service/creditRiskRecord"),
        meta: {
          title: "征信报告",
          icon: "",
          activeMenu: "/service/creditRisk",
          parentTitle: "征信报告",
          parentRouteName: "serviceRisk",
        },
      },
      {
        path: "reportCredit",
        name: "serviceReportCredit",
        component: () => import("@/views/service/reportCredit"),
        meta: { title: "上报征信", icon: "", keepAlive: true },
      },
      {
        path: "reportCreditRecord",
        name: "serviceReportCreditRecord",
        hidden: true,
        component: () => import("@/views/service/reportCreditRecord"),
        meta: {
          title: "上报征信记录",
          icon: "",
          activeMenu: "/service/reportCredit",
          parentTitle: "学历风控",
          parentRouteName: "serviceReportCredit",
        },
      },
      {
        path: "booleaRiskRecord",
        name: "serviceBooleaRiskRecord",
        hidden: true,
        component: () => import("@/views/service/booleaRiskRecord"),
        meta: {
          title: "风控记录",
          icon: "",
          activeMenu: "/service/booleaRisk",
          parentTitle: "百融风控",
          parentRouteName: "serviceRisk",
        },
      },
    ],
  },
  {
    path: "/order",
    name: "order",
    component: Layout,
    redirect: "/order/list",
    meta: { title: "订单管理", icon: "order" },
    children: [
      {
        path: "list",
        name: "orderList",
        component: () => import("@/views/order/list"),
        meta: { title: "订单列表", icon: "" },
      },
      {
        path: "hb",
        name: "orderHb",
        component: () => import("@/views/order/hb"),
        meta: { title: "花呗订单", icon: "" },
      },
      {
        path: "buyout",
        name: "orderBuyout",
        component: () => import("@/views/order/buyout"),
        meta: { title: "买断订单", icon: "" },
      },
      {
        path: "unpay",
        name: "orderUnpay",
        component: () => import("@/views/order/unpay"),
        meta: { title: "签约待支付订单", icon: "" },
      },
      {
        path: "relet",
        name: "orderRelet",
        component: () => import("@/views/order/relet"),
        meta: { title: "续租订单", icon: "" },
      },
    ],
  },
  {
    path: "/refund",
    name: "refund",
    component: Layout,
    redirect: "/refund/list",
    meta: { title: "退款管理", icon: "refund" },
    children: [
      {
        path: "list",
        name: "refundList",
        component: () => import("@/views/refund/list"),
        meta: { title: "仅退款列表", icon: "" },
      },
      {
        path: "refundDetails/:id",
        name: "refundDetail",
        hidden: true,
        props: true,
        component: () => import("@/views/refund/refundDetails"),
        meta: {
          title: "退款详情",
          icon: "",
          activeMenu: "/refund/list",
          parentTitle: "仅退款列表",
          parentRouteName: "refund",
        },
      },
    ],
  },
  {
    path: "/goods",
    name: "goods",
    component: Layout,
    redirect: "/goods/index",
    meta: { title: "商品管理", icon: "product" },
    children: [
      {
        path: "index",
        name: "goodsIndex",
        component: () => import("@/views/goods/index"),
        meta: { title: "商品列表", icon: "", keepAlive: true },
      },
      {
        path: "release/:draftId?",
        name: "goodsAdd",
        hidden: true,
        props: true,
        component: () => import("@/views/goods/add"),
        meta: {
          title: "发布商品",
          icon: "",
          activeMenu: "/goods/index",
          parentTitle: "商品列表",
          parentRouteName: "goodsIndex",
        },
      },
      {
        path: "view/:goodsId",
        name: "goodsView",
        hidden: true,
        props: true,
        component: () => import("@/views/goods/view"),
        meta: {
          title: "查看商品",
          icon: "",
          activeMenu: "/goods/index",
          parentTitle: "商品列表",
          parentRouteName: "goodsIndex",
        },
      },
      {
        path: "success/:goodsId/:isEdit?",
        name: "goodsSuccess",
        hidden: true,
        props: true,
        component: () => import("@/views/goods/success"),
        meta: {
          title: "发布商品",
          icon: "",
          activeMenu: "/goods/index",
          parentTitle: "商品列表",
          parentRouteName: "goodsIndex",
        },
      },
      {
        path: "draft",
        name: "goodsDraft",
        hidden: true,
        component: () => import("@/views/goods/draft"),
        meta: {
          title: "草稿箱",
          icon: "",
          activeMenu: "/goods/index",
          parentTitle: "商品列表",
          parentRouteName: "goodsIndex",
        },
      },
      {
        path: "baseEdit/:goodsId",
        name: "goodsBaseEdit",
        hidden: true,
        props: true,
        component: () => import("@/views/goods/edit"),
        meta: {
          title: "编辑商品信息",
          icon: "",
          activeMenu: "/goods/index",
          parentTitle: "商品列表",
          parentRouteName: "goodsIndex",
        },
      },
      {
        path: "meal/:goodsId/:pact_type/:mealId?",
        name: "goodsMeal",
        hidden: true,
        props: true,
        component: () => import("@/views/goods/edit"),
        meta: {
          title: "编辑套餐",
          icon: "",
          activeMenu: "/goods/index",
          parentTitle: "商品列表",
          parentRouteName: "goodsIndex",
        },
      },
      {
        path: "mealInfo/:goodsId",
        name: "goodsMealInfo",
        hidden: true,
        props: true,
        component: () => import("@/views/goods/mealList"),
        meta: {
          title: "套餐信息",
          icon: "",
          activeMenu: "/goods/index",
          parentTitle: "商品列表",
          parentRouteName: "goodsIndex",
        },
      },
      {
        path: "category",
        name: "goodsCategory",
        component: () => import("@/views/category/index"),
        meta: { title: "品类管理", icon: "" },
      },
      {
        path: "add",
        name: "goodsCategoryApply",
        hidden: true,
        component: () => import("@/views/category/add"),
        meta: {
          title: "申请品类",
          icon: "",
          activeMenu: "/goods/category",
          parentTitle: "品类管理",
          parentRouteName: "goodsCategory",
        },
      },
    ],
  },
  {
    path: "/account",
    name: "account",
    component: Layout,
    redirect: "/account/list",
    meta: { title: "账号管理", icon: "user" },
    children: [
      {
        path: "list",
        name: "accountList",
        component: () => import("@/views/system/account/index"),
        meta: { title: "账号列表", icon: "" },
      },
    ],
  },
  {
    path: "/permission",
    name: "permission",
    component: Layout,
    redirect: "/permission/role",
    meta: { title: "权限管理", icon: "permission" },
    children: [
      {
        path: "role",
        name: "permissionRole",
        component: () => import("@/views/system/role/index"),
        meta: { title: "角色列表", icon: "" },
      },
    ],
  },
  {
    path: "/shop",
    name: "shop",
    component: Layout,
    redirect: "/shop/returnAd",
    meta: { title: "店铺管理", icon: "shop" },
    children: [
      {
        path: "returnAd",
        name: "shopReturnAd",
        component: () => import("@/views/shop/returnAd"),
        meta: { title: "归还地址设置", icon: "" },
      },
      {
        path: "addReturnAd/:editId?",
        name: "shopAddReturnAd",
        hidden: true,
        props: true,
        component: () => import("@/views/shop/addReturnAd"),
        meta: {
          title: "添加归还地址",
          icon: "",
          activeMenu: "/shop/returnAd",
          parentTitle: "归还地址设置",
          parentRouteName: "shopReturnAd",
        },
      },
      {
        path: "rentAd",
        name: "shopRentAd",
        component: () => import("@/views/shop/rentAd"),
        meta: { title: "租赁地址", icon: "" },
      },
      {
        path: "info",
        name: "shopInfo",
        component: () => import("@/views/shop/info"),
        meta: { title: "店铺信息", icon: "" },
      },
      {
        path: "shop",
        name: "shopShop",
        component: () => import("@/views/shop/shop"),
        meta: { title: "旺铺管理", icon: "" },
      },
      {
        path: "manage/:editId/:editName",
        name: "shopManage",
        hidden: true,
        props: true,
        component: () => import("@/views/shop/manage"),
        meta: {
          title: "关联商品",
          icon: "",
          activeMenu: "/shop/shop",
          parentTitle: "旺铺管理",
          parentRouteName: "shopShop",
        },
      },
    ],
  },
  {
    path: "/examine",
    name: "examine",
    component: Layout,
    redirect: "/examine/list",
    meta: { title: "审核管理", icon: "user" },
    children: [
      {
        path: "list",
        name: "examineList",
        component: () => import("@/views/examine/index"),
        meta: { title: "待审核", icon: "", keepAlive: true },
      },
      {
        path: "pay",
        name: "examinePay",
        component: () => import("@/views/examine/pay"),
        meta: { title: "审核付款", icon: "", keepAlive: true }
      },
      {
        path: "notSend",
        name: "examineSend",
        component: () => import("@/views/examine/notSend"),
        meta: { title: "待发货", icon: "", keepAlive: true }
      },
      {
        path: "send",
        name: "examineAlready",
        component: () => import("@/views/examine/send"),
        meta: { title: "已发货", icon: "", keepAlive: true }
      },
      {
        path: "deal",
        name: "examineDeal",
        component: () => import("@/views/examine/deal"),
        meta: { title: "已发货", icon: "", keepAlive: true }
      }
    ],
  },
  {
    path: "/procure",
    name: "procure",
    component: Layout,
    redirect: "/procure/quotation",
    meta: { title: "采购管理", icon: "procure" },
    children: [
      {
        path: "quotation",
        name: "procureQuotation",
        component: () => import("@/views/procure/index"),
        meta: { title: "申请采购", icon: "" },
      },
      {
        path: "list",
        name: "procureList",
        component: () => import("@/views/procure/list"),
        meta: { title: "采购列表", icon: "" },
      },
    ],
  },
  {
    path: "/bill",
    name: "bill",
    component: Layout,
    redirect: "/bill/list",
    meta: { title: "账单管理", icon: "bill" },
    children: [
      {
        path: "list",
        name: "billList",
        component: () => import("@/views/bill/index"),
        meta: { title: "订单财务列表", icon: "" },
      },
      {
        path: "bill",
        name: "billBill",
        component: () => import("@/views/bill/bill"),
        meta: { title: "账单财务列表", icon: "" },
      },
      {
        path: "hb",
        name: "billHb",
        component: () => import("@/views/bill/hb"),
        meta: { title: "花呗分期账单", icon: "" },
      },
      {
        path: "device",
        name: "billDevice",
        component: () => import("@/views/bill/device"),
        meta: { title: "设备分期导出", icon: "" },
      },
      {
        path: "payment",
        name: "billPayment",
        component: () => import("@/views/bill/payment"),
        meta: { title: "回款导出", icon: "" },
      },
    ],
  },
  {
    path: "/overdue",
    name: "overdue",
    component: Layout,
    redirect: "/overdue/list",
    meta: { title: "逾期管理", icon: "overdue" },
    children: [
      {
        path: "list",
        name: "overdueList",
        component: () => import("@/views/overdue/index"),
        meta: { title: "逾期列表", icon: "" },
      },
    ],
  },
  {
    path: "/my",
    name: "my",
    component: Layout,
    redirect: "/my/index",
    meta: { title: "账户管理", icon: "user" },
    children: [
      {
        path: "index",
        name: "myIndex",
        component: () => import("@/views/account/index"),
        meta: { title: "我的账户", icon: "" },
      },
      {
        path: "statistics",
        name: "myStatistics",
        component: () => import("@/views/account/statistics"),
        meta: { title: "账户统计", icon: "" },
      },
      {
        path: "withdrawal",
        name: "myWithdrawal",
        component: () => import("@/views/account/withdrawal"),
        meta: { title: "提现记录", icon: "" },
      },
      {
        path: "flow",
        name: "myFlow",
        component: () => import("@/views/account/flow"),
        meta: { title: "对账流水", icon: "" },
      },
      {
        path: "withdraw/:withdrawId?",
        name: "myWithdraw",
        hidden: true,
        props: true,
        component: () => import("@/views/account/withdraw"),
        meta: {
          title: "详情",
          icon: "",
          activeMenu: "/my/withdrawal",
          parentTitle: "提现记录",
          parentRouteName: "myWithdrawal",
        },
      },
    ],
  },
  // 404 page must be placed at the end !!!
  { path: "*", redirect: "/404", hidden: true },
];

const createRouter = () =>
  new Router({
    mode: "history", // require service support
    scrollBehavior(to, from, savedPosition) {
      if (savedPosition) {
        return savedPosition;
      } else {
        return { x: 0, y: 0 };
      }
    },
    routes: constantRoutes,
  });

const router = createRouter();

export function resetRouter() {
  const newRouter = createRouter();
  router.matcher = newRouter.matcher; // reset router
}

export default router;
